common = {

    //开发配置
    //LOGIN_URL: 'http://192.168.0.200:8090/artisan/',
    //API_URL: 'http://192.168.0.200:8080/wx/',

    //线上配置
    LOGIN_URL: 'http://api.b.gumaor.com/artisan/',
    API_URL: 'http://api.wx.gumaor.com/',


    tip:function(msg){
      api.toast({msg: msg, duration: 1500, location: 'bottom'});
    },
    gotoLogin:function(){
      api.openWin({
          name: 'login',
          url: './html/login.html',
          animation:{type:'none'},
          slidBackEnabled: false,
          pageParam: {name: 'test'}
      });
    },
    getToday:function(){
      var d = new Date();
      var year = d.getFullYear();
      var month = d.getMonth()+1;
      var day = d.getDate();
      if(month<10){month = '0'+month;}
      if(day<10){day = '0'+day;}
      return  year+ '-' + month + '-' + day;
    },
    //转换音频时长，用于显示 秒 转换为 00:00
    getPeriodShow: function(seconds) {
        var minutes = Math.floor(seconds % 3600 / 60);
        var seconds = Math.floor(seconds % 3600 % 60);
        if (minutes < 10) { minutes = "0" + minutes; }
        if (seconds < 10) { seconds = "0" + seconds; }
        return minutes + ":" + seconds;
    },
};

ajax = {
    get: function(_url, _data, _cb) {
        var user = $api.getStorage('user');
        api.ajax({
            url: common.API_URL + _url,
            method: 'get',
            headers: {token: user.token},
            timeout: 5,
            cache:false,
            data: {values: _data}
        }, function(res, err) {
            ajax.cb(res, err, _cb);
        });
    },
    post: function(_url, _data, _cb) {
        var user = $api.getStorage('user');
        api.ajax({
            url: common.API_URL + _url,
            method: 'post',
            headers: {token: user.token},
            timeout: 5,
            data: {values: _data}
        }, function(res, err) {
            ajax.cb(res, err, _cb);
        });
    },
    login:function(_url, _data, _cb){
      api.ajax({
          url: common.LOGIN_URL + _url,
          method: 'post',
          timeout: 5,
          data: {values: _data}
      }, function(res, err) {
        api.hideProgress();
        if (res) {
            if(res.code==0){
                _cb && _cb(res);
            }else {
              api.toast({msg: res.msg, duration: 1500, location: 'middle'});
            }
        } else {
          api.alert({title: '温馨提示',  msg: err.msg});
        }
      });
    },
    download: function (_url, savePath, _cb) {
      api.download({
          url: _url,
          savePath: savePath,
          cache: true,
          allowResume: true
      }, function(ret, err) {
          if (ret.state) {
              _cb&&_cb(ret);
          } else {
              console.log(JSON.stringify(err));
          }
      });
    },
    cb: function(res, err, _cb) {
        if (res) {
            //console.log(JSON.stringify(res));
            if(res.code==100403){
              $api.rmStorage('last_login_day');
              common.checkUserLogin();
            }else{
              _cb && _cb(res);
            }
        } else {
          api.hideProgress();
          api.cancelAjax({tag: 'publish'});
          api.alert({title: '温馨提示', msg: err.msg});
        }
    }
};
